// pages/shoppingCart/shoppingCart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isshow:"hide",
    purchaseServiceshopindex:"",
    purchaseServicegoodsindex:"",
    purchaseServiceList:[],
    // 店铺列表
    shopList:[
      {
        ischeck:false,
        shopName:"苏宁自营",
        goodsList:[
          {
            ischeck:false,
            goodsImage:"/image/images/Pikachu.gif",
            goodsName:"皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘皮卡丘",
            goodsInfo:"8GB+256GB",
            goodsColor:"耀金黑",
            goodsPrice:"408.33",
            goodsNum:"1",
            goodsinventory:"99",
            goodsrestriction:"10",
            purchaseService:[
              {
                purchaseServiceName:"过期补贴",
                purchaseServiceLIst:[
                  {
                    name:"囤货无忧补贴6月",
                    price:"5.00",
                    active:false,
                  },
                  {
                    name:"囤货无忧补贴5月",
                    price:"6.00",
                    active:false,
                  },
                ]
              },
              {
                purchaseServiceName:"过期换新",
                purchaseServiceLIst:[
                  {
                    name:"囤货无忧换新6月",
                    price:"5.00",
                    active:false,
                  },
                  {
                    name:"囤货无忧换新5月",
                    price:"6.00",
                    active:false,
                  },
                ]
              }
            ]
          },
          {
            ischeck:false,
            goodsImage:"/image/images/pidiguo.png",
            goodsName:"打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机打印机",
            goodsInfo:"8GB+256GB",
            goodsColor:"耀金黑",
            goodsPrice:"79.22",
            goodsNum:"1",
            goodsinventory:"50"
          },
          {
            ischeck:false,
            goodsImage:"/image/images/lecc.jpg",
            goodsName:"拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机拖地机",
            goodsInfo:"8GB+256GB",
            goodsColor:"耀金黑",
            goodsPrice:"50.55",
            goodsNum:"1",
            goodsinventory:"50",
            goodsrestriction:"20"
          }
        ]
      },
      {
        ischeck:false,
        shopName:"班长没我帅",
        goodsList:[
          {
            ischeck:false,
            goodsImage:"/image/images/iphone_p.jpg",
            goodsName:"空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调空调",
            goodsInfo:"8GB+256GB",
            goodsColor:"耀金黑",
            goodsPrice:"88.66",
            goodsNum:"1",
            goodsinventory:"99999"
          },
          {
            ischeck:false,
            goodsImage:"/image/images/4_08.gif",
            goodsName:"呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈呼啦圈",
            goodsInfo:"8GB+256GB",
            goodsColor:"耀金黑",
            goodsPrice:"2.78",
            goodsNum:"1",
            goodsinventory:"888"
          },
          {
            ischeck:false,
            goodsImage:"/image/images/4_03.gif",
            goodsName:"绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽绿豆芽",
            goodsInfo:"8GB+256GB",
            goodsColor:"耀金黑",
            goodsPrice:"3",
            goodsNum:"1",
            goodsinventory:"6",//库存    
            goodsrestriction:"50"//最大购买量
          }
        ]
      }
    ],
    shopListCopy:[],//深拷贝后的数据
    checkAllCopy:"",//保存全选的状态
    // 是否编辑
    isEdit:false,
    // 全选状态
    checkall:false,
    // 总价
    priceAll:0,
    // 总数量
    numAll:0,
  },
  // 去商品详情
  toshopDetail(){
    let _self = this;
    wx.navigateTo({
      url: '/pages/shopDetails/shopDetails',
    })
  },
  // 弹窗的确定
  purchaseServiceBtn(){
    let _self = this;
    _self.data.shopList[_self.data.purchaseServiceshopindex].goodsList[_self.data.purchaseServicegoodsindex].purchaseService = _self.data.purchaseServiceList;
    _self.setData({
      shopList:_self.data.shopList
    })
    _self.backGroundanimation()
  },
  // 弹窗的选项变化
  purchaseServiceLIstindexClick(e){
    let _self = this;
    let {purchaseserviceindex , purchaseservicelistindex} = e.currentTarget.dataset;
    for(let index in _self.data.purchaseServiceList[purchaseserviceindex].purchaseServiceLIst){
      if(index != purchaseservicelistindex){
        _self.data.purchaseServiceList[purchaseserviceindex].purchaseServiceLIst[index].active= false;
      }
    }
    _self.data.purchaseServiceList[purchaseserviceindex].purchaseServiceLIst[purchaseservicelistindex].active= 
    !_self.data.purchaseServiceList[purchaseserviceindex].purchaseServiceLIst[purchaseservicelistindex].active;
    _self.setData({
      purchaseServiceList:_self.data.purchaseServiceList
    })


  },
  // 动画隐藏
  backGroundanimation(){
    let _self = this;
    _self.data.isshow = "hide";
    _self.animate.translateY(350).step();
    _self.setData({
      isshow :_self.data.isshow,
      animationData:_self.animate.export()
    })
  },
  purchaseServiceFn(e){
    let {goodsindex , shopindex} = e.currentTarget.dataset;
    let _self = this;
    _self.data.purchaseServicegoodsindex = goodsindex;
    _self.data.purchaseServiceshopindex = shopindex;
    _self.data.isshow = "show";
    _self.data.purchaseServiceList = _self.data.shopList[shopindex].goodsList[goodsindex].purchaseService;
    // 创建一个动画
    let animation = wx.createAnimation({
      timingFunction:"ease-in-out"
    })
    // this=>小程序当前实例化对象
    // this.data 定义数据
    console.log(this)
    // 把小程序的实例化对象中的动画里放置我们创建的动画
    _self.animate = animation;
    animation.translateY(-350).step();

    _self.setData({
      isshow :_self.data.isshow,
      purchaseServiceList:_self.data.purchaseServiceList,
      animationData:_self.animate.export()
    })
  },
  // 删除商品
  deleteShop(){
    let _self = this;
    // 判断商品有没有被选中
    let type = false;
    for(let item of _self.data.shopList){
      for(let items of item.goodsList){
        if(items.ischeck){
          type = true
        }
      }
    }
    if(!type){
        wx.showToast({
          title: '请选择商品',
          icon:"none",
          duration:2000
        })
    }else{
      wx.showModal({
        title: '提示！',
        cancelText:"确定",
        confirmText:"取消",
        cancelColor:"#ff0",
        confirmColor:"#f00",
        content: '是否确定删除',
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            // console.log('用户点击取消')
            // splice(下标 ，从该下标开始删几个)
            for(let index = _self.data.shopList.length-1;index>=0;index--){
              if(_self.data.shopList[index].ischeck){
                _self.data.shopList.splice(index,1)
                _self.data.shopListCopy.splice(index,1)
              }else{
                for(let indexx = _self.data.shopList[index].goodsList.length -1;indexx>=0;indexx--){
                  if(_self.data.shopList[index].goodsList[indexx].ischeck){
                    _self.data.shopList[index].goodsList.splice(indexx,1)
                  _self.data.shopListCopy[index].goodsList.splice(indexx,1)
                  }
                }
              }
            }
            _self.data.isEdit = !_self.data.isEdit;
            _self.PriceNum();
            _self.setData({
              isEdit:_self.data.isEdit,
              shopList:_self.data.shopListCopy,
              checkall:_self.data.checkall
            })
          }
        }
      })
    }
  },
  // 编辑切换
  toEdit(){
    let _self =this;
    if(!_self.data.isEdit){
      _self.data.shopListCopy = JSON.parse(JSON.stringify(_self.data.shopList));;//深拷贝
      _self.data.checkAllCopy = _self.data.checkall;
      for(let item of _self.data.shopList){
        // 所有的状态全都变成false
        item.ischeck = false;
        for(let items of item.goodsList){
          items.ischeck = false
        }
      }
      _self.data.checkall = false;
      _self.setData({
        shopList:_self.data.shopList,
        checkall:_self.data.checkall
      })
    }else{
      _self.setData({
        shopList:_self.data.shopListCopy,
        checkall:_self.data.checkAllCopy
      })
    }
    _self.data.isEdit =!_self.data.isEdit;
    
    _self.setData({
      isEdit:_self.data.isEdit
    })
  },
  // 输入框失去焦点
  bindblur(e){
    let _self = this;
    let value = e.detail.value;
    if(value =="" || value == 0){
      _self.setData({
        shopList:_self.data.shopList
      })
    }
  },
  // 用户输入时候
  bindinput(e){
    console.log(e)
    let _self = this;
    let {goodsindex , shopindex} = e.currentTarget.dataset;
    let value = e.detail.value*1;
    if(
      value>0
      && 
      value<999
      && 
      value<=parseInt(_self.data.shopList[shopindex].goodsList[goodsindex].goodsinventory)
      && (_self.data.shopList[shopindex].goodsList[goodsindex].goodsrestriction?value<=parseInt(_self.data.shopList[shopindex].goodsList[goodsindex].goodsrestriction):true)
      ){
        _self.data.shopList[shopindex].goodsList[goodsindex].goodsNum = value
      }else if(
        _self.data.shopList[shopindex].goodsList[goodsindex].goodsrestriction?parseInt(_self.data.shopList[shopindex].goodsList[goodsindex].goodsinventory)<=parseInt(_self.data.shopList[shopindex].goodsList[goodsindex].goodsrestriction):true
        &&
        parseInt(_self.data.shopList[shopindex].goodsList[goodsindex].goodsinventory)<=value
        &&
        parseInt(_self.data.shopList[shopindex].goodsList[goodsindex].goodsinventory)<=999
      ){
        _self.data.shopList[shopindex].goodsList[goodsindex].goodsNum= _self.data.shopList[shopindex].goodsList[goodsindex].goodsinventory;
      }else if(_self.data.shopList[shopindex].goodsList[goodsindex].goodsrestriction){
          if(
            parseInt(_self.data.shopList[shopindex].goodsList[goodsindex].goodsrestriction)<= parseInt(_self.data.shopList[shopindex].goodsList[goodsindex].goodsinventory)
            &&
            parseInt(_self.data.shopList[shopindex].goodsList[goodsindex].goodsrestriction)<=value
            &&
            parseInt(_self.data.shopList[shopindex].goodsList[goodsindex].goodsrestriction)<=999
          ){
            _self.data.shopList[shopindex].goodsList[goodsindex].goodsNum= _self.data.shopList[shopindex].goodsList[goodsindex].goodsrestriction;
          }
      }else if(value >999){
        _self.data.shopList[shopindex].goodsList[goodsindex].goodsNum = 
        _self.inventoryFn(
          parseInt(_self.data.shopList[shopindex].goodsList[goodsindex].goodsinventory),
          parseInt(_self.data.shopList[shopindex].goodsList[goodsindex].goodsrestriction)
        ) ||
        // 库存
        _self.goodsrestrictionFn(
          parseInt(_self.data.shopList[shopindex].goodsList[goodsindex].goodsinventory),
          parseInt(_self.data.shopList[shopindex].goodsList[goodsindex].goodsrestriction)
        ) ||
        // 最大购买量
        999
        // 999
      }
     if(value != 0){
        _self.data.shopList[shopindex].goodsList[goodsindex].ischeck = true;
        // 判断店铺是否选中
        _self.shopIsCheck(shopindex)
        // 判断全选是否选中
        _self.shopcheckAll()
        // 总价总数量要重新计算
        _self.PriceNum()
        _self.setData({
        shopList:_self.data.shopList,
        checkall:_self.data.checkall
        })
     }
    
  },
  // 判断最大购买量
  goodsrestrictionFn(a , b){
      if(b){
        if(b<999 && a>b){
          return b
        }
      }else{
        false
      }
  },
  // 判断库存
  inventoryFn(a,b){
    if(b){
      if(a<999&& a<b){
        return a
      }else{
        return false
      }
    }else{
        if(a<999){
          return a
        }else{
          return false
        }
    }

  },
  // 全选
  checkAll(){
    let _self = this;
    _self.data.checkall =!_self.data.checkall;
    for(let item of _self.data.shopList){
      item.ischeck = _self.data.checkall;
      for(let itemgoods of item.goodsList){
        itemgoods.ischeck = _self.data.checkall
      }
    }
    _self.PriceNum()
    _self.setData({
      shopList:_self.data.shopList,
      checkall:_self.data.checkall
    })
  },
  shopCheck(e){
    let {goodsindex , shopindex} = e.currentTarget.dataset;
    let _self = this;
    _self.data.shopList[shopindex].goodsList[goodsindex].ischeck =!_self.data.shopList[shopindex].goodsList[goodsindex].ischeck;

   // 判断店铺是否选中
       _self.shopIsCheck(shopindex)
       // 判断全选是否选中
       _self.shopcheckAll()
       // 总价总数量要重新计算
       _self.PriceNum()
       _self.setData({
        shopList:_self.data.shopList,
        checkall:_self.data.checkall
       })
  },
  minuteFn(e){
    let _self = this;
    let {goodsindex , shopindex} = e.currentTarget.dataset;
    if(_self.data.shopList[shopindex].goodsList[goodsindex].goodsNum>1){
      _self.data.shopList[shopindex].goodsList[goodsindex].goodsNum--;
      _self.data.shopList[shopindex].goodsList[goodsindex].ischeck = true;
       // 判断店铺是否选中
       _self.shopIsCheck(shopindex)
       // 判断全选是否选中
       _self.shopcheckAll()
       // 总价总数量要重新计算
       _self.PriceNum()
       _self.setData({
        shopList:_self.data.shopList,
        checkall:_self.data.checkall
       })
    }
  },
  // 加号
  plusFn(e){
    let _self = this;//this当前小程序对象
    console.log(e)
    let {goodsindex , shopindex} = e.currentTarget.dataset;
    // let num = _self.data.shopList[shopindex].goodsList[goodsindex].goodsNum;goodsrestriction goodsinventory
    if(_self.data.shopList[shopindex].goodsList[goodsindex].goodsinventory<
      (_self.data.shopList[shopindex].goodsList[goodsindex].goodsrestriction?_self.data.shopList[shopindex].goodsList[goodsindex].goodsrestriction:999999999999999999999999999999)){
        if(_self.data.shopList[shopindex].goodsList[goodsindex].goodsinventory>_self.data.shopList[shopindex].goodsList[goodsindex].goodsNum){
          _self.data.shopList[shopindex].goodsList[goodsindex].goodsNum++;
          _self.data.shopList[shopindex].goodsList[goodsindex].ischeck = true;
        }
      }
        if(_self.data.shopList[shopindex].goodsList[goodsindex].goodsrestriction && _self.data.shopList[shopindex].goodsList[goodsindex].goodsrestriction<_self.data.shopList[shopindex].goodsList[goodsindex].goodsinventory){
          if(_self.data.shopList[shopindex].goodsList[goodsindex].goodsrestriction>_self.data.shopList[shopindex].goodsList[goodsindex].goodsNum){
            _self.data.shopList[shopindex].goodsList[goodsindex].goodsNum++;
          _self.data.shopList[shopindex].goodsList[goodsindex].ischeck = true;
          }
        
      }

      // 判断店铺是否选中
      _self.shopIsCheck(shopindex)
      // 判断全选是否选中
      _self.shopcheckAll()
      // 总价总数量要重新计算
      _self.PriceNum()
      _self.setData({
       shopList:_self.data.shopList,
       checkall:_self.data.checkall
      })
    
  },
  // 计算总价总数量
  PriceNum(){
    let _self = this;
    let price = 0;
    let num = 0;
    for(let item of _self.data.shopList){
      for(let goodsitem of item.goodsList){
        if(goodsitem.ischeck){
          price += (goodsitem.goodsNum*1) * (goodsitem.goodsPrice*1);
          num += (goodsitem.goodsNum*1)
        }
      }
    }
    _self.data.priceAll= price.toFixed(2);
    _self.data.numAll = num;
    _self.setData({
      priceAll:_self.data.priceAll,
      numAll:_self.data.numAll
    })
  },
  // 店铺的单选 
  shopCheckbtn(e){
    let _self = this;
    let {  shopindex} = e.currentTarget.dataset;
    _self.data.shopList[shopindex].ischeck =!_self.data.shopList[shopindex].ischeck;
    for(let item of _self.data.shopList[shopindex].goodsList){
      item.ischeck= _self.data.shopList[shopindex].ischeck
    }
    _self.shopcheckAll();
    _self.PriceNum()
    _self.setData({
      shopList:_self.data.shopList,
      checkall:_self.data.checkall
    })
  },
  // 全选被选中
  shopcheckAll(){
    let _self = this;
    let type = false;
    for(let item of _self.data.shopList){
      if(!item.ischeck){
        type = true
      }
    }
    if(!type){
      _self.data.checkall = true
    }else{
      _self.data.checkall = false
    }
  },
  // 店铺被选中
  shopIsCheck(shopindex){
    let _self = this;//_that _this _a
    let type = false;
    for(let item of _self.data.shopList[shopindex].goodsList){
      if(!item.ischeck){
        type = true
      }
    }
    if(!type){
      _self.data.shopList[shopindex].ischeck = true
    }else{
      _self.data.shopList[shopindex].ischeck = false
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})